using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Decav.Gatsb.Application;
using Decav.Gatsb;
using Decav.Gatsb.Data;
using Decav.Security;
using Decav.Reality;
using Decav.Gatsb.Web;

public partial class CompleteSetup : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        GatsbWebApplication.EnsureAuthenticatedSession();

        serverErrorMsgDiv.Visible = false;
    }

    protected void SubmitButton_Click(object sender, EventArgs e)
    {
        Page.Validate();
        if (!IsValid || !CustomValidate())
            return;

        using (IGatsbDataAdapter adapt = GatsbApplication.Current.GetDataAdapter())
        {
            if (adapt.CheckUserExists(new EmailAddress(emailTextBox.Text)))
            {
                SetError("A user with that email address already exists.  If you forgot your password, <a href=\"ResetPassword.aspx\">reset it here</a>.");
                return;
            }
        }

        UpdateUser();
        FormsAuthentication.SetAuthCookie(GatsbApplication.Current.CurrentUser.Email.ToString(), false);
        GatsbWebApplication.Current.NewAccountHasNotSeenHelp = true;
        HttpContext.Current.Response.Redirect("~/Home.aspx");
    }

    private bool CustomValidate()
    {
        if (!termsOfUseCheck.Checked)
        {
            SetError("Please agree to the terms of use before continuing.");
            return false;
        }

        if (emailTextBox.Text.Trim().Length == 0)
        {
            SetError("Gatsb requires a valid email address to provide you with a login and notifications.");
            return false;
        }

        if (!birthdayBox.IsValid || birthdayBox.Value < DateTime.Today.AddYears(-100))
        {
            SetError("Please enter your birthday in Month/Day/Year (ex: 2/14/1986) format.");
            return false;
        }

        if (confirmPasswordTextBox.Text != passwordTextBox.Text)
        {
            SetError("Your password and confirmation password must match.");
            return false;
        }

        PersonName name = PersonName.Empty;
        if (!PersonName.TryParse(nameTextBox.Text, out name))
        {
            SetError("Please tell us your first and last name.  You may use an initial for your last name if you wish.");
            return false;
        }

        try
        {
            new EmailAddress(emailTextBox.Text);
        }
        catch
        {
            SetError("Gatsb requires a valid email address to provide you with a login and notifications.");
            return false;
        }

        if (passwordTextBox.Text.Trim().Length == 0)
        {
            SetError("Please create a password for yourself.  Your password may be 4-20 characters long, and contain any characters.");
            return false;
        }

        if (postalCodeTextBox.Text.Trim().Length != 5)
        {
            SetError("Please supply us with your postal code so we can find places around your area.");
            return false;
        }

        return true;
    }

    private void SetError(string message)
    {
        serverErrorMsgDiv.InnerHtml = message;
        serverErrorMsgDiv.Visible = true;
    }

    private void UpdateUser()
    {
        using(IGatsbDataAdapter adapt = GatsbApplication.Current.GetDataAdapter())
        {
            Decav.Gatsb.User usr = GatsbApplication.Current.CurrentUser;
            usr.Email = new Decav.Reality.EmailAddress(emailTextBox.Text);
            usr.PostalCode = postalCodeTextBox.Text;
            usr.Name = PersonName.Parse(nameTextBox.Text);

            UserProfile profile = adapt.GetUserProfile(usr.Id);
            profile.BirthdayDate = birthdayBox.Value;

            adapt.UpdateUserProfile(usr, profile);
            adapt.ChangeUserPassword(HashedPassword.Empty, HashedPassword.HashPassword(passwordTextBox.Text));
        }
    }
}
